// 获取全局应用程序实例对象
const app = getApp()

var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: false,
      last_time: '重新发送',
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      is_show: true,
      last_time: countdown + '秒后重新发送',
    })

    countdown--;

  }
  setTimeout(function () {
    settime(that)
  }
    , 1000)
}

Page({
  data: {
    phone: '',
    vcode: '',
    last_time: '获取验证码',
    is_show: true,
    is_disabled: true,
    openid: ''
  },

  onLoad: function () {
    console.log(app.globalData.info.openid)
  },

  //手机号
  phoneInput: function (e) {
    var re = /^1\d{10}$/;    //正则表达式
    if (re.test(e.detail.value)) {
      this.setData({
        is_show: false,
        phone: e.detail.value
      })
    } else {
      this.setData({
        is_show: true
      })
    }
  },

  //验证码
  vcodeInput: function (e) {
    this.setData({
      vcode: e.detail.value
    })
  },
  vcodeTap: function () {
    var that = this;
    var params = {
      openid: app.globalData.info.openid,
      phone: this.data.phone
    }
    //发起请求  
    app.network.POST(
      'https://www.ausck.com/api/vcode',
      {
        params: params,
        success: function (res) {
          //拿到解密后的数据，进行代码逻辑 
          that.setData({
            is_disabled: false
          })
        }
      })
    // 获取验证码按钮倒计时
    settime(that);
  },

  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },

  submitTap: function () {
    var that = this;
    var params = {
      openid: app.globalData.info.openid,
      phone: this.data.phone,
      vcode: this.data.vcode,
    }
    //发起请求  
    app.network.POST(
      'https://www.ausck.com/api/reg',
      {
        params: params,
        success: function (res) {
          //拿到解密后的数据，进行代码逻辑 
          if (res.data.status == 200) {
            app.globalData.info.is_rz=1;
            wx.navigateTo({
              url: '/pages/index/index'
            })
          } else {
            setTimeout(() => {
              that.setData(
                { popErrorMsg: "验证码错误,认证失败!" }
              );
              that.ohShitfadeOut();
              return;
            }, 100)
          }
        }
      })
  }
})
